<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>入库单管理</title>
		<link rel="stylesheet" href="/static/layui/css/layui.css" />
<link rel="stylesheet" href="/static/ui/css/ui.css" />
	</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<div class="layui-input-inline" style="width: 100px;">
							<input type="text" name="<goods_name>" placeholder="物料名称" class="layui-input">
						</div>
						<div class="layui-input-inline" style="width: 50px;">
							<input type="text" name="id" placeholder="单号" class="layui-input">
						</div>
						<div class="layui-input-inline" style="width: 100px;">
							<input type="text" name="in_no" placeholder="编码" class="layui-input">
						</div>
						<div class="layui-input-inline" style="width: 100px;">
							<input type="text" name="model" placeholder="型号" class="layui-input">
						</div>
						<div class="layui-input-inline" style="width: 100px;">
							<select name="is_lock">
								<option value="">选择状态</option>
								<option value="0">未锁定</option>
								<option value="1">已锁定</option>
							</select>
						</div>
						<div class="layui-input-inline" style="width: 100px;">
							<select name="cate_id" lay-search id="cate-area">
								<option value="">选择分类</option>
							</select>
						</div>
						<div class="layui-input-inline" style="width: 100px;">
							<select name="area_id" lay-search id="area-area" lay-filter="area_select">
								<option value="">选择仓库</option>
							</select>
						</div>
						<div class="layui-input-inline" style="width: 100px;">
							<select name="bar_id" lay-search id="bar-area">
								<option value="">选择货架</option>
							</select>
						</div>
						<div class="layui-inline">
							<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit
								lay-filter="stock_in-query">
								<i class="layui-icon layui-icon-search"></i>
								查询
							</button>
							<button type="reset" class="pear-btn pear-btn-md" lay-filter="stock_in-reset" lay-submit>
								<i class="layui-icon layui-icon-refresh"></i>
								重置
							</button>
						
						</div>
					</div>
				</form>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="stock_in-table" lay-filter="stock_in-table"></table>
			</div>
		</div>

		<script type="text/html" id="stock_in-toolbar">
			<button class="layui-btn layui-btn-primary layui-border-red layui-btn-sm" lay-event="add">
				<i class="layui-icon layui-icon-add-1"></i>
				新增入库单
			</button>
		</script>

		<script type="text/html" id="stock_in-bar">
			<button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i
					class="layui-icon layui-icon-edit"></i></button>
			<button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i
					class="layui-icon layui-icon-delete"></i></button>
		</script>
		<script type="text/html" id="stock_in-time">
		{{layui.util.toDateString(d.add_time*1000, 'yyyy-MM-dd HH:mm:ss')}}
		</script>
		<script type="text/html" id="stock_in-lock">
		{{# if(d.is_lock < 1){ }}
		<i class="layui-icon layui-icon-ok-circle" style="color: #1E9FFF;"></i> 
		{{# }else{ }}
		<i class="layui-icon layui-icon-close-fill" style="color: #ff0000;"></i> 
		{{# } }}
		</script>
		<script src="../config.js"></script>
		<script src="/static/layui/layui.js"></script>
		<script src="/static/ui/ui.js"></script>
		<script>
			layui.use(['table', 'form', 'jquery', 'common'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let common = layui.common;

				let MODULE_PATH = "./";

				let cols = [
					[{
							title: 'ID',
							field: 'id',
							align: 'center',
							minWidth : '60',
						},
						{
							title: '物料名称',
							field: 'goods_name',
							align: 'center',
							minWidth : '120',
						},
						{
							title: '编码',
							field: 'in_no',
							align: 'center',
							minWidth : '80',
						},
						{
							title: '数量',
							field: 'stock_num',
							align: 'center',
							minWidth : '60',
						},
						{
							title: '型号',
							field: 'model',
							align: 'center',
							minWidth : '80',
						},
						{
							title: '分类',
							field: 'cname',
							align: 'center',
							minWidth : '100',
						},
						{
							title: '仓库',
							field: 'addrname',
							align: 'center',
							minWidth : '100',
						},
						{
							title: '货架',
							field: 'barname',
							align: 'center',
							minWidth : '100',
						},
						{
							title: '锁定',
							field: 'is_lock',
							align: 'center',
							templet: '#stock_in-lock',
							minWidth : '100',
						},
						{
							title: '添加时间',
							field: 'add_time',
							align: 'center',
							minWidth : '100',
							//templet: '#stock_in-time'
						},
						{
							title: '操作',
							toolbar: '#stock_in-bar',
							align: 'center',
							width: 130,
							fixed : 'right'
						}
					]
				]
				let cats = [],
					area = [];
				table.render({
					elem: '#stock_in-table',
					url: apiUrl + 'stock_in/list',
					headers: getHeader(),
					parseData: function(res) {
						//console.log(res)
						cats = res.data.cates;
						area = res.data.area;
						return {
							"code": res.code, //解析接口状态
							"msg": res.message, //解析提示文本
							"count": res.data.count, //解析数据长度
							"data": res.data.list //解析数据列表
						};
					},
					page: true,
					cols: cols,
					skin: 'line',

					toolbar: '#stock_in-toolbar',
					defaultToolbar: [{
						title: '刷新',
						layEvent: 'refresh',
						icon: 'layui-icon-refresh',
					}, 'filter', 'print', 'exports']
				});
				setTimeout(() => {
					cats.forEach(el => {
						$("#cate-area").append(new Option(el.name, el.id));
					})
					area.forEach(el => {
						$("#area-area").append(new Option(el.name, el.id));
					})
					form.render("select");
				}, 1000)

				table.on('tool(stock_in-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'edit') {
						window.edit(obj);
					}
				});

				table.on('toolbar(stock_in-table)', function(obj) {
					if (obj.event === 'add') {
						window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					}
				});
				form.on('select(area_select)', function(data) {
					let area_id = data.value*1; //获取下拉值
					//console.log(area_id); //获取下拉列表ID的值
					if(area_id < 1) {
						$("#bar-area").empty()
						$("#bar-area").html('<option value="">选择货架</option>')
						form.render("select");
						return;
					}
					let areaData = area.find(d => d.id == area_id)
					if(areaData) {
						$("#bar-area").empty()
						$("#bar-area").html('<option value="">选择货架</option>')
						areaData.child.forEach(el => {
							$("#bar-area").append(new Option(el.name, el.id));
						})
						form.render("select");
					}

				});
				// 监听搜索操作

				form.on('submit(stock_in-query)', function(data) {
					table.reload('stock_in-table', {
						where: {
							param: $('form').serialize()
						},
						page: {
							curr: 1
						}
					})
					return false;
				});
				form.on('submit(stock_in-reset)', function(data) {
					table.reload('stock_in-table', {
						where: {
							param: ''
						},
						page: {
							curr: 1
						}
					});
					return false;
				})

				window.add = function() {
					layer.open({
						type: 2,
						title: '选择物料',
						shade: 0.1,
						offset: 'rt',
						area: ['90%', '100%'],
						anim: 1,
						maxmin: true,
						content: MODULE_PATH + 'goods.html',
						end: (index) => {
							table.reload('stock_in-table');
						}
					});

				}

				window.edit = function(obj) {
					layer.open({
						type: 2,
						title: '修改',
						shade: 0.1,
						offset: 'rt',
						area: ['80%', '100%'],
						anim: 1,
						maxmin: true,
						content: MODULE_PATH + 'edit.html?id=' + obj.data.id
					});
				}
				window.remove = function(obj) {
					layer.confirm('确定要删除', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						_post(layui, 'stock_in/del', {
							id: obj.data.id
						}, res => {
							layer.msg('删除成功', {
								icon: 1,
								time: 1000
							}, () => {
								obj.del();
							});
						}, err => {
							//layer.msg('删除失败',{icon:2,time:1000});
						})
					});
				}

				$('#addBtn').click(e => {

				})
				window.refresh = function(param) {
					table.reload('stock_in-table');
				}
			})
		</script>
	</body>
</html>
